<template>
	<view><button class="authorization" open-type="getUserInfo" @getuserinfo="appLogin">您暂未授权，点击页面任何地方授权</button></view>
</template>

<script>
import utils from '../../../static/common/utils.js';
import newToken from '../../../static/http/pageConfig.js';
import value from '../../../static/value/value.js';
export default {
	data() {
		return {
			querys: {}
		};
	},
	onLoad(query) {
		this.querys = {
			query: query,
			path: 'pages/index/index',
			scene: uni.getLaunchOptionsSync().scene
		};
	},
	methods: {
		// 请求token
		GetUserToken(code, iv, encryptedData) {
			const that = this;
			var data = {
				code: code,
				iv: iv,
				encryptedData: encryptedData,
				options: JSON.stringify(that.querys),
				ticks: utils.utils.GetTicks(),
				channel: this.$tokenObj.obj.channel
			};
			data.sign = utils.utils.GetSign(data);
			// 获取用户信息
			this.$http
				.request({
					url: `/Login/Oauth`,
					method: 'post',
					data: data
				})
				.then(res => {
					that.$emit('appLogin', true);
					newToken.obj.token = res.data.token;
					that.showBtn = res.data.token;
					uni.setStorageSync('token', res.data.token);

					// uni.setStorage({
					// 	data: res.data.token,
					// 	key: 'token'
					// });
				})
				.catch(err => {
					console.log(err);
				});
		},
		appLogin: function() {
			const that = this;
			uni.getProvider({
				service: 'oauth',
				success: function(res) {
					//支持微信、qq和微博等
					if (~res.provider.indexOf('weixin')) {
						uni.login({
							provider: 'weixin',
							success: function(loginRes) {
								// 获取用户信息
								uni.getUserInfo({
									provider: 'weixin',
									success: function(infoRes) {
										that.GetUserToken(loginRes.code, infoRes.iv, infoRes.encryptedData);
									},
									fail: function(err) {
										console.log(err);
									}
								});
							}
						});
					}
				}
			});
		}
	}
};
</script>

<style scoped>
.authorization {
	position: fixed;
	text-align: center;
	width: 100%;
	height: 100%;
	padding-top: 50%;
}
</style>
